let MapSDK = require("../../qqmapSDK/qqmap-wx-jssdk.min");
let qqMap;
Page({
  data: {

  },
  onLoad() {
    qqMap = new MapSDK({
      key: "FSWBZ-A6XRU-JVRVC-BFKUI-D5KTJ-76BUU"
    })
    //坑!!!!!!!!!!!!!!!!!!!!
    this.setData({
      search: this.search.bind(this) //坑
    })
  },
  search(value) {

    return new Promise((resolve, reject) => {
      //1.获取用户输入的关键字
      //2.调用腾讯地图的api,获得补全的文字
      console.log(value === "")
      if (value === "") {
        resolve([])
        return //中断逻辑
      }
      qqMap.getSuggestion({
        keyword: value,
        region: "南京", //限定城市名字
        region_fix: 1, //如果搜索不到,那么扩大选取到全国
        success(res) { //成功的回调
          console.log(res)
          //处理符合searchBar的数据
          let result = res.data.map(item => ({
            text: item.title,
            value: {
              longitude: item.location.lng,
              latitude: item.location.lat,
              address: item.address
            }
          }))
          //将处理好的数据回传给result
          resolve(result)

        },
        fail(err) { //失败的回调
          console.log(err)
        }
      })

      //3.点击选项的时候,进行导航

    })
  },
  selectResult(sel) {
    console.log(sel)
    let {
      value: {
        longitude,
        latitude,
        address
      },
      text: name
    } = sel.detail.item

    console.log(longitude, latitude, address, name);

    //调用微信打开地理位置
    wx.openLocation({
      latitude,
      longitude,
      name,
      address,
      scale: 15,
      success() {
        console.log('打开成功');
      },
      fail(err) {
        console.log(err);
      }
    })
  }
})